package cn.stylefeng.guns.modular.tool.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.tool.entity.ToolWork;
import cn.stylefeng.guns.modular.tool.mapper.ToolWorkMapper;
import cn.stylefeng.guns.modular.tool.model.params.ToolWorkParam;
import cn.stylefeng.guns.modular.tool.model.result.ToolWorkResult;
import  cn.stylefeng.guns.modular.tool.service.ToolWorkService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 云仓派工表 服务实现类
 * </p>
 * @author zhucx
 * @since 2023-04-18
 */
@Service
public class ToolWorkServiceImpl extends ServiceImpl<ToolWorkMapper, ToolWork> implements ToolWorkService {

    @Override
    public void add(ToolWorkParam param){
        ToolWork entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(ToolWorkParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(ToolWorkParam param){
        ToolWork oldEntity = getOldEntity(param);
        ToolWork newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public ToolWorkResult findBySpec(ToolWorkParam param){
        return this.baseMapper.findBySpec(param);
    }

    @Override
    public List<ToolWorkResult> findListBySpec(ToolWorkParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(ToolWorkParam param,String condition){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param,condition);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(ToolWorkParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private ToolWork getOldEntity(ToolWorkParam param) {
        return this.getById(getKey(param));
    }

    private ToolWork getEntity(ToolWorkParam param) {
        ToolWork entity = new ToolWork();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

	@Override
	public void deleteBySpec(ToolWorkParam toolWorkParam) {
		this.baseMapper.deleteBySpec(toolWorkParam);
	}

	@Override
	public Integer addReturnId(ToolWorkParam toolWorkParam) {
		return this.baseMapper.addReturnId(toolWorkParam);
	}

}
